package com.syqc.choose.car

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import autodispose2.autoDispose
import com.syqc.comlib.auto.AutoDisposeViewModel
import com.syqc.entity.HistoryCar
import com.syqc.entity.HistoryCars
import com.syqc.net.ServiceHelper
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers

/**
 *@author  hfjs
 *@time   2025/5/6 15:46
 *@desc
 */
class HistoryVM : AutoDisposeViewModel() {
    private val _carLive = MutableLiveData<HistoryCar>()
    val carLive: LiveData<HistoryCar> = _carLive

    private val _carsLive = MutableLiveData<MutableList<HistoryCars.ListBean>?>()
    val carsLive: LiveData<MutableList<HistoryCars.ListBean>?> = _carsLive

    var carInfo: HistoryCars.ListBean?= null

    var isVideoCar = false
    var isVideo = false
    var isChange = false

    init {
        ChooseCars.listObservable.hide().autoDispose(this).subscribe {
            handleCars(it)
        }
    }

    private fun handleCars(cars: MutableList<HistoryCars.ListBean>) {
        toastLoading(true)
        Observable.fromIterable(cars.withIndex())
            .flatMap { (index, car) ->
                getVerify(car.carId).toObservable()
                    .map { result -> Triple(car, result, index) }
                    .subscribeOn(Schedulers.io())
            }
            .toList().observeOn(AndroidSchedulers.mainThread()).autoDispose(this)
            .subscribe({ result ->
                val list =
                    result.filter { it.second == "true" }.sortedBy { it.third }.map { it.first }
                _carsLive.postValue(list.toMutableList())
                toastLoading(false)
            }, {
                toastLoading(false)
            })
    }


    fun chooseCar() {
        if (isVideo || isVideoCar) getCarInfo(carInfo?.carId!!)
        else if (isChange) changeTeam(carInfo?.carId!!)
        else handleInfo(null)
    }

    /**
     * 查询是否有当前车辆权限
     *
     * @param carId
     */
    private fun getVerify(carId: String) = ServiceHelper.getRepository().getVerify(carId)

    /**
     * 查询车辆信息
     *
     * @param carId
     */
    private fun getCarInfo(carId: String) {
        ServiceHelper.getRepository().getCarInfo(carId).autoDispose(this).subscribe({
            it.run {
                ChooseCars.addCars(
                    carId,
                    carName,
                    carPlate,
                    iconLink,
                    videos,
                    !videos.isNullOrEmpty()
                )
                val list = HistoryCar(
                    carId,
                    carName,
                    carPlate,
                    iconLink,
                    videos,
                    !videos.isNullOrEmpty(),
                    teamId,teamName,
                )
                _carLive.postValue(list)
            }
        }, {
            handleInfo(null)
        })
    }

    /**
     * 查询车队
     *
     * @param carId
     */
    private fun changeTeam(carId: String) {
        ServiceHelper.getRepository().canChangeTeam(carId).autoDispose(this).subscribe({
            handleInfo(!it.moditeam)
        }, {
            handleInfo(null)
        })
    }

    private fun handleInfo(noAuth: Boolean? = false) {
        carInfo?.run {
            ChooseCars.addCars(
                carId = carId,
                carPlate = carPlate,
                carName = carName,
                videos = videos,
                iconLink = iconLink?:"",
                isVideo = !videos.isNullOrEmpty()
            )
            val list = HistoryCar(
                carId,
                carName,
                carPlate,
                iconLink,
                videos,
                !videos.isNullOrEmpty(),
                noAuth.toString()
            )
            _carLive.postValue(list)
        }

    }
}